package com.ziki.test.crypto;

/*
 * 字符串 DESede(3DES) 加密
 */
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Des3 {
    
    private static final String Algorithm = "DESede"; // 定义 加密算法,可用
                                                      
    // DES,DESede,Blowfish
    // keybyte为加密密钥，长度为24字节
    // src为被加密的数据缓冲区（源）
    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
        try {
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            // 加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    
    // keybyte为加密密钥，长度为24字节
    // src为加密后的缓冲区
    public static byte[] decryptMode(byte[] keybyte, byte[] src) {
        try {
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            
            // 解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
        Des3 d = new Des3();
        d.test1();
        d.test2();
    }
    
    private void test1() {
        // 添加新安全算法,如果用JCE就要把它添加进去
        // Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String key = "+x8BPL54cUo8KgL87z0W+cHU2Suq8jkD";
        byte[] keyBytes = Base64.base64decode(key);
        String szSrc = "10669995$15311779656$W$2008-08-19 11:40:00";
        
        System.out.println("加密前的字符串:" + szSrc);
        
        byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
        System.out.println("加密后的base64字符串:" + Base64.base64encode(encoded));
        
        byte[] srcBytes = decryptMode(keyBytes, encoded);
        System.out.println("解密后的字符串:" + (new String(srcBytes)));
    }
    
    private void test2() {
        // 添加新安全算法,如果用JCE就要把它添加进去
        // Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String key = "27jrWz2sxrVbR+pnyg6jWHhgNk4sZo46";
        byte[] keyBytes = Base64.base64decode(key);
        String szSrc = "3033$13311083060$D10$2004-02-24 16:19:27";
        
        System.out.println("加密前的字符串:" + szSrc);
        
        byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
        System.out.println("加密后的base64字符串:" + Base64.base64encode(encoded));
        
        byte[] srcBytes = decryptMode(keyBytes, encoded);
        System.out.println("解密后的字符串:" + (new String(srcBytes)));
    }
}
